package com.fast.develop.api.rest.registor;

import com.fast.develop.api.rest.SchemaRest;
import com.fast.develop.core.enums.RequestMethod;
import com.fast.develop.core.properties.DomainRuntimeProperties;
import com.fast.develop.core.web.AbstractMappingRegistor;
import com.fast.develop.core.web.MappingInfo;
import com.fast.develop.core.web.MappingRegistor;
import com.fast.develop.integration.web.MvcMappingHandlerMapping;
import lombok.SneakyThrows;


/**
 * @author 莫寅
 * @createTime 2022-06-18 04:52
 * @desc：
 */
public class SchemaMappingRegistor extends AbstractMappingRegistor implements MappingRegistor {


    public static final String KEY = "/schemas";

    public SchemaMappingRegistor(MvcMappingHandlerMapping mvcMappingHandlerMapping, DomainRuntimeProperties domainRuntimeProperties, SchemaRest schemaRest) {
        super(mvcMappingHandlerMapping, domainRuntimeProperties, schemaRest);
    }


    @Override
    @SneakyThrows
    public void registerMapping() {
        MappingInfo getSchemas = new MappingInfo(new String[]{getPath("{type}")}, new RequestMethod[]{RequestMethod.GET});
        registerMapping(getSchemas, SchemaRest.class.getDeclaredMethod("getSchemas", String.class));

        MappingInfo getMetaSchemas = new MappingInfo(new String[]{getPath("/meta/{type}")}, new RequestMethod[]{RequestMethod.POST});
        registerMapping(getMetaSchemas, SchemaRest.class.getDeclaredMethod("getMetaSchemas", String.class, String.class));

        MappingInfo create = new MappingInfo(new String[]{getPath("{meta}")}, new RequestMethod[]{RequestMethod.POST});
        registerMapping(create, SchemaRest.class.getDeclaredMethod("create", String.class, String.class));

        MappingInfo createDoaminField = new MappingInfo(new String[]{getPath("{meta}/fields")}, new RequestMethod[]{RequestMethod.POST});
        registerMapping(createDoaminField, SchemaRest.class.getDeclaredMethod("createDoaminField", String.class, String.class));

        MappingInfo write = new MappingInfo(new String[]{getPath("{meta}")}, new RequestMethod[]{RequestMethod.PUT});
        registerMapping(write, SchemaRest.class.getDeclaredMethod("write", String.class, String.class, String.class));

        MappingInfo enable = new MappingInfo(new String[]{getPath("{meta}/enable")}, new RequestMethod[]{RequestMethod.PUT});
        registerMapping(enable, SchemaRest.class.getDeclaredMethod("enable", String.class, String.class));

        MappingInfo disable = new MappingInfo(new String[]{getPath("{meta}/disable")}, new RequestMethod[]{RequestMethod.PUT});
        registerMapping(disable, SchemaRest.class.getDeclaredMethod("disable", String.class, String.class));
    }

    @Override
    public String type() {
        return KEY;
    }

    @Override
    protected String getKey() {
        return KEY;
    }
}
